var app = getApp();
let goodsList = [
  { actEndTime: '2022/05/01 10:00:43' }

]
Page({
  data: {

  },
  onLoad: function (e) {
    var order_id = e.order_id;
    this.setData({
      order_id: order_id
    })
    this.getPinTuanOrderInfo();
    wx.getSystemInfo({
      success: (res) => {
        this.setData({
          pixelRatio: res.pixelRatio,
          windowHeight: res.windowHeight,
          windowWidth: res.windowWidth
        })
      },
    })
  },
  getPinTuanOrderInfo: function () {
    var t = this;
    app.visit("account/getPinTuanOrderInfo", {
      order_id: t.data.order_id
    }, "", function (c) {
      console.log(c)
      if (0 == c.code) {
        let goodsList = c.data.goodsList
        t.setData({
          goodsList: goodsList,
          group_order: c.data.group_order,
          group_user: c.data.group_user,
        })
        let endTimeList = [];
        // 将活动的结束时间参数提成一个单独的数组，方便操作
        goodsList.forEach(o => { endTimeList.push(o.actEndTime) })
        t.setData({ actEndTimeList: endTimeList });
        // 执行倒计时函数
        t.countDown();
      }
    })
  },
  timeFormat(param) {//小于10的格式化函数
    return param < 10 ? '0' + param : param;
  },
  countDown() {//倒计时函数
    // 获取当前时间，同时得到活动结束时间数组
    let newTime = new Date().getTime();
    let endTimeList = this.data.actEndTimeList;
    let countDownArr = [];

    // 对结束时间进行处理渲染到页面
    endTimeList.forEach(o => {
      let endTime = new Date(o).getTime();
      let obj = null;
      // 如果活动未结束，对时间进行处理
      if (endTime - newTime > 0) {
        let time = (endTime - newTime) / 1000;
        // 获取天、时、分、秒
        let hou = parseInt(time % (60 * 60 * 24) / 3600);
        let min = parseInt(time % (60 * 60 * 24) % 3600 / 60);
        let sec = parseInt(time % (60 * 60 * 24) % 3600 % 60);
        obj = {
          hou: this.timeFormat(hou),
          min: this.timeFormat(min),
          sec: this.timeFormat(sec)
        }
      } else {//活动已结束，全部设置为'00'
        obj = {
          hou: '00',
          min: '00',
          sec: '00'
        }
      }
      countDownArr.push(obj);
    })
    // 渲染，然后每隔一秒执行一次倒计时函数
    this.setData({ countDownList: countDownArr })
    setTimeout(this.countDown, 1000);
  },
  onShareAppMessage: function () {
    var t = this
    var group_order = t.data.group_order;
    var num = Number(group_order.ct_num) - Number(group_order.yct_num);
    let title = '【还差' + num + '人即可成团】快来帮我获得该套餐哟';
    return {
      title: title,
      path: "/pages/new/pintuan_xiangqing?order_id=" + group_order.id + '&t_user_id=' + wx.getStorageSync("user_id"),
      imageUrl: group_order.img,
      success: (res) => {
        // 分享成功
      },
      fail: (res) => {
        // 分享失败
      }
    }
  },
  pt_detail_order: function (e) {
    var order_id = e.currentTarget.dataset.order_id
    wx.navigateTo({
      url: 'pt_detail_order?order_id='+order_id
    });
  }
})